package mvc
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	
	public class ClockData extends EventDispatcher
	{
		private var _time:Time;
		private var _timer:Timer;
		private var _realTime:Boolean;
		private var _startTime:uint;
		
		public function ClockData(target:IEventDispatcher=null)
		{
			super(target);
			this._startTime = getTimer();
		}

		public function get time():Time
		{
			if(_realTime || _time == null){
				var date:Date;
				if(_time == null){
					date = new Date;
				}else{
					date = new Date(null,null,null,_time.hour,_time.minute,_time.hour);
					date.milliseconds = getTimer() - _startTime;
				}
				return new Time(date.hours,date.minutes,date.seconds);
			}else{
				return _time.clone();
			}
		}

		public function set time(value:Time):void
		{
			_time = value.clone();
			dispatchEvent(new Event(Event.CHANGE));
		}

		public function get realTime():Boolean
		{
			return _realTime;
		}

		public function set realTime(value:Boolean):void
		{
			_realTime = value;
			if(value){
				if(_timer==null){
					_timer = new Timer(1000,0);
					_timer.addEventListener(TimerEvent.TIMER,onTimer);
				}
				if(!_timer.running){
					_timer.start();
				}
			}else{
				if(!_timer.running){
					_timer.stop();
				}
			}
		}
		private function onTimer(event:TimerEvent):void{
			dispatchEvent(new Event(Event.CHANGE));
		}


	}
}